package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.pojo.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工管理")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO 登录参数
     * @return 返回结果
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();
        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return 返回结果
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     *
     * @param employeeDTO 新增员工数据
     * @return 是否添加成功
     */
    @PostMapping
    @ApiOperation("员工新增")
    public Result<String> insert(@Validated @RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工，员工数据：{}", employeeDTO);
        employeeService.insert(employeeDTO);

        return Result.success();
    }

    /**
     * 员工分页查询
     *
     * @param queryDTO 查询参数
     * @return 分页查询结果
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> query(EmployeePageQueryDTO queryDTO) {
        log.info("员工分页查询，查询条件为：{}", queryDTO);
        queryDTO.check();
        PageResult pageResult = employeeService.query(queryDTO);

        log.info("{}", pageResult);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询员工信息
     *
     * @param id 传入要查询的id
     * @return 返回该id的员工信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工信息")
    public Result<Employee> queryById(@PathVariable Integer id) {
        log.info("根据id查询员工信息,id为{}", id);
        Employee employee = employeeService.queryById(id);
        return Result.success(employee);
    }

    /**
     * 启用禁用员工账号
     * @param id 需要操作的账号
     * @param status 需要设置的属性
     * @return 返回
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用员工账号")
    public Result<String> setStatus(Integer id, @PathVariable Integer status) {
        log.info("启用禁用员工账号{}{}", status, id);
        employeeService.setStatus(id, status);
        return Result.success();
    }

    /**
     * 修改员工信息
     * @param employeeDTO 传入的员工信息
     * @return 返回结果
     */
    @PutMapping
    @ApiOperation("修改员工信息")
    public Result<String> update(@RequestBody EmployeeDTO employeeDTO) {
        log.info("修改员工信息参数:{}", employeeDTO);
        employeeService.update(employeeDTO);

        return Result.success();
    }


}
